﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Gao.Poker.Command;
using Gao.Poker.Domain;
using Gao.Poker.Services;

namespace Gao.Poker.ViewModel
{
    public class MemberMangeViewModel : BaseViewModel
    {
        public Action<Member> PrintAction { get; set; }
        public ICommand SaveCommand { get; set; }
        private List<Member> _members;
        private int _memberTotal;
        private int _pageIndex;
       
        private Member _Member;

        public String MemberName { get; set; }
        public String MemberCardNo { get; set; }
        public List<GameRecord> _membersGameRecords;
        public List<RechargeLog> _rechargeLogs;

        public List<GameRecord> MembersGameRecords
        {
            get
            {
                _membersGameRecords = new MemberService().GetGameRecord(Member.CardNo);
                return _membersGameRecords;
            }
            set { _membersGameRecords = value; }
        }

        public List<RechargeLog> RechargeLogs
        {
            get
            {
                _rechargeLogs = RechargeLogService.Instance().GetMemberRechargeLog(Member.ID);
                return _rechargeLogs;
            }
            set { _rechargeLogs = value; }
        }


        public virtual Member Member
        {
            get { return _Member ?? (_Member = new Member()); }
            set
            {
                _Member = value;
                OnPropertyChanged("Member");
                OnPropertyChanged("MembersGameRecords");
                OnPropertyChanged("RechargeLogs");
            }
        }

        public MemberMangeViewModel()
        {
            MemberName = String.Empty;
            MemberCardNo = String.Empty;
            SaveCommand = new MemberSaveCommand(new Action<Boolean,Member>(SaveResult));
        }

        private void SaveResult(bool obj,Member member)
        {
            if (obj)
            {
                OnPropertyChanged("Members");
                if (Message != null)
                    Message("数据保存成功");
                if (PrintAction != null)
                    PrintAction(member);
            }
        }

        public List<Member> Members
        {
            get
            {
                _members = new MemberService().SearchMembers(MemberName, MemberCardNo,PageIndex,20);
                OnPropertyChanged("MemberTotal");
                return _members;
            }
        }

        public int MemberTotal
        {
            get
            {
                _memberTotal = MemberService.Instance().SearchMembersTotal(MemberName, MemberCardNo);
                return _memberTotal;
            }
        }

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                OnPropertyChanged("Members");
            }
        }
    }
}
